/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;

/**
 *
 * @author CALDERON
 */

import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.HibernateUtilZoologia;
import muhnes.sizoo.modelo.Terrestre;

public class TerrestreDaoImpl implements TerrestreDao{
       Session sesion;
    
    public TerrestreDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }

    @Override
    public void guardar( Terrestre terrestre ) {
        Transaction transaccion = sesion.beginTransaction();
       sesion.save( terrestre );
        transaccion.commit();
    }

        
    @Override
    public void guardarColeccion( List< Terrestre > terrestres ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( terrestres );
        transaccion.commit();
    }

    @Override
    public void actualizar( Terrestre terrestre ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( terrestre );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Terrestre > terrestres ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( terrestres );
        transaccion.commit();
    }

    @Override
    public void eliminar( Terrestre terrestre ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( terrestre );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Terrestre > terrestres ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( terrestres );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Terrestre terrestre = buscarPorId( id );
        sesion.delete( terrestre );
        transaccion.commit();
    }

    @Override
    public Terrestre buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Terrestre where codigoacuatico = " + id);
        Terrestre terrestre = (Terrestre)consulta.uniqueResult();
        transaccion.commit();
        return terrestre;
    }

    @Override
    public List< Terrestre > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Terrestre");
        List< Terrestre > terrestres = consulta.list();
        transaccion.commit();
        return terrestres;
    } 
}
